package zk.tool.handler;

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import zk.tool.App;
import zk.tool.ZKClient;
import zk.tool.model.TreeItemData;
import zk.tool.ui.AddNodeController;
import zk.tool.util.AlertUtil;
import zk.tool.util.StageUtil;

import java.io.IOException;

public class AddTreeItemClickHandler extends AbstractTreeContextMenuClickHandler<TreeItemData> {
    private static final Logger log = LoggerFactory.getLogger(AddTreeItemClickHandler.class);

    public AddTreeItemClickHandler(TreeView<TreeItemData> treeView, ZKClient client) {
        super(treeView, client);
    }

    @Override
    public void handle0(ActionEvent event, TreeItem<TreeItemData> selectedItem) {
        openAddWindow(selectedItem);
    }

    private void openAddWindow(TreeItem<TreeItemData> selectedItem) {
        try {
            Stage stage = StageUtil.createDialogStage(App.primaryStage, "新增节点");
            FXMLLoader loader = StageUtil.loadFxml("addForm.fxml");
            AnchorPane root = loader.<AnchorPane>load();
            Scene scene = new Scene(root, 400, 300);
            AddNodeController controller = loader.<AddNodeController>getController();
            controller.setParentItem(selectedItem);
            controller.setStage(stage);
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            log.error("open add window exception: ", e);
            AlertUtil.exception("打开窗口失败", e);
        }
    }

}
